/*
This file is part of buco.  
Copyright (c) 2012 Christopher Pawelzik <christopher@cpawelzik.com>

Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 
*/ 

(function() {

function CompositionController() { 
	this.self = this;
	this.addAction("composition.new", this.newComposition);
	this.addAction("composition.checkIntegrity", this.checkIntegrity); 
	this.addAction("composition.addBundleDialog", this.addBundleDialog); 
	this.addAction("composition.addBundle", this.addBundle); 	
	this.addAction("composition.removeBundle", this.removeBundle); 	
	this.addAction("composition.load", this.load); 	
	this.addAction("composition.selectBundle", this.selectBundle); 
	this.addAction("composition.showWire", this.showWire); 
	this.addAction("composition.toggleWire", this.toggleWire); 
	this.addAction("composition.deploy", this.deploy); 
	this.addAction("composition.undo", this.undo); 
	this.addAction("composition.redo", this.redo); 
	this.addAction("composition.importDialog", this.importDialog); 
	this.addAction("composition.importComposition", this.importComposition); 
}

CompositionController.prototype = new buco.mvc.Controller();

CompositionController.prototype.undo = function() { 	
	buco.rpc.undo({
		onSuccess: function(composition){
			if(composition != null) {
				// Store the new composition locally 
				buco.composition = composition; 			
				// Show view to render the composition 			
				var view = new BundleCompositionView(buco.composition); 			
				view.show($("#composition")); 	
			}
		}
	}); 
}

CompositionController.prototype.redo = function() { 	
	buco.rpc.redo({
		onSuccess: function(composition){
			if(composition != null) {
				// Store the new composition locally 
				buco.composition = composition; 			
				// Show view to render the composition 			
				var view = new BundleCompositionView(buco.composition); 			
				view.show($("#composition")); 	
			}
		}
	}); 
}

CompositionController.prototype.removeBundle = function() { 	
	if(buco.selectedBundle == null) { 
		alert("Please select a bundle first."); 
	}	
	buco.rpc.removeBundle({
		params: {bundleID: buco.selectedBundle.bundleID},
		onSuccess: function(result) { 
			buco.selectedBundle = null; 
			$("#bundleInfo").empty(); 
			$("#bclView").empty(); 			
			CompositionController.prototype.load.call(this);
		}
	});
}

CompositionController.prototype.showWire = function(connection) { 	
	var from = parseInt(connection.sourceId.replace("bundle", ""));
	var to = parseInt(connection.targetId.replace("bundle", ""));
	var wire = buco.helper.getWire(from, to); 
	if(wire != null) { 
		var view = new BundleWireView(wire, connection); 
		view.show($(".ui-layout-east")); 
	}
}

CompositionController.prototype.toggleWire = function(obj) { 		
	buco.rpc.toggleWire({
		params: {from: obj.wire.from, to: obj.wire.to},
		onSuccess: function(result) { 
			var isActive = result.isActive;
			var color = isActive ? "black" : "#D8D8D8"; 
			obj.connection.setPaintStyle({ strokeStyle: color, lineWidth:2 });
			obj.wire.isActive = result.isActive;
			// Refresh the view
			var view = new BundleWireView(obj.wire, obj.connection); 
			view.show($(".ui-layout-east")); 
			// Show warnings						
			if(result.messages.length > 0) { 				
				var view = new SemanticMessagesView(result.messages); 
				view.show($("#output")); 
			}
		}
	});
}

CompositionController.prototype.selectBundle = function(bundleID) {			
	// Get the bundle from the local composition object 
	var bundle = buco.helper.getBundle(bundleID); 
	// Show bundle view 
	if(bundle != null) { 
		buco.selectedBundle = bundle; 
		var view = new BundleView(bundle); 
		view.show($(".ui-layout-east")); 
	}	
}

CompositionController.prototype.newComposition = function() {	
	buco.rpc.newComposition({
		onSuccess: function(result) { 
			CompositionController.prototype.load.call(this);
			$("#bundleInfo").empty(); 
			$("#bclView").empty(); 			
		}
	}); 
}

CompositionController.prototype.checkIntegrity = function() {	
	buco.rpc.checkIntegrity({
		onSuccess: function(warnings) { 			
			var view = new SemanticMessagesView(warnings); 
			view.show($("#output")); 
		}
	});
}

// Load composition from server
CompositionController.prototype.load = function() {			
	buco.rpc.getComposition({ 		
		onSuccess: function(composition) {	
			// Store the new composition locally 
			buco.composition = composition; 			
			// Show view to render the composition 			
			var view = new BundleCompositionView(buco.composition); 			
			view.show($("#composition")); 			
		}		
	 }); 	
}

CompositionController.prototype.addBundleDialog = function() {	
	$("#addBundleDialog").dialog("open"); 
}

CompositionController.prototype.importDialog = function() {	
	$("#importDialog").dialog("open"); 
}

CompositionController.prototype.importComposition = function() {	
	// Read the file 
	var file = document.getElementById('zipFileToImport').files[0];	
	if(file != null) { 				
		var reader = new FileReader();				
		reader.readAsDataURL(file); 
		reader.onload = function(event) { 	
			var content = event.target.result;			
			buco.rpc.importComposition({
				params: {name: file.name, content: content}, 
				onError: function(error) { 
					$("#importDialog").dialog('close')					
					alert("error importing composition"); 
				},
				onSuccess: function(composition) { 														
					$("#importDialog").dialog('close')					
					// Refresh composition					
					buco.composition = composition; 			
					// Show view to render the composition 			
					var view = new BundleCompositionView(buco.composition); 			
					view.show($("#composition")); 										
				}
			}); 
		};
	}	
}

CompositionController.prototype.addBundle = function() {	
	// Read the file 
	var file = document.getElementById('jarFileToUpload').files[0];	
	if(file != null) { 				
		var reader = new FileReader();				
		reader.readAsDataURL(file); 
		reader.onload = function(event) { 	
			var content = event.target.result;			
			buco.rpc.addBundle({
				params: {name: file.name, content: content}, 
				onError: function(error) { 
					$("#addBundleDialog").dialog('close')					
					alert(error.message); 
				},
				onSuccess: function(result) { 														
					$("#addBundleDialog").dialog('close')					
					// Refresh composition
					buco.rpc.getComposition({ 		
						onSuccess: function(composition) {	
							// Store the new composition locally 
							buco.composition = composition; 			
							// Show view to render the composition 			
							var view = new BundleCompositionView(buco.composition); 			
							view.show($("#composition")); 			
							// Show warnings						
							if(result.length > 0) { 
								var view = new SemanticMessagesView(result); 
								view.show($("#output")); 
							}
						}		
					}); 								
				}
			}); 
		};
	}	
}

return new CompositionController(); 
}) ();